/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyIM.client.view.form;

import MyIM.client.engine.Engine;
import MyIM.client.view.ContactsView;
import MyIM.client.view.component.HintTextField;
import MyIM.client.view.component.JTKButton;
import MyIM.client.view.lnf.MetroStyle;
import MyIM.component.Group;
import MyIM.utilities.Status;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;

/**
 *
 * @author Acay13
 */
public class Dialog_EditGroup extends JDialog implements ActionListener {

    private HintTextField txtGroupName;
    private JButton btnSave, btnCancel;
    private Engine engine;
    private Dialog_AddFriend diaglogAddFriend;
    private ContactsView contactsView;
    private Group group;

    public Dialog_EditGroup(Group group, ContactsView contactsView, Engine engine) {
        this.group = group;
        this.engine = engine;
        this.contactsView = contactsView;
        diaglogAddFriend = null;
        init();
        initListener();
    }

    private void init() {
        // Initializes the frame
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        // Initializes the components
        txtGroupName = new HintTextField("Group's name", 10);

        btnSave = new JButton("Edit this group");
        btnSave.setActionCommand("save");
        btnSave.setPressedIcon(new ImageIcon(new BufferedImage(1,
                1, BufferedImage.TYPE_INT_ARGB_PRE)));
        btnSave.setVerticalTextPosition(JButton.CENTER);
        btnSave.setHorizontalTextPosition(JButton.CENTER);
        btnSave.setFont(MetroStyle.BUTTON_FONT);

        getRootPane().setDefaultButton(btnSave);

        btnCancel = new JTKButton("Cancel?");
        btnCancel.setActionCommand("cancel");
        btnCancel.setPressedIcon(new ImageIcon(new BufferedImage(1,
                1, BufferedImage.TYPE_INT_ARGB_PRE)));
        btnCancel.setVerticalTextPosition(JButton.CENTER);
        btnCancel.setHorizontalTextPosition(JButton.CENTER);
        btnCancel.setFont(MetroStyle.BUTTON_FONT);

        // Does layout
        initLayout();
        setResizable(false);
        pack();
        setLocationRelativeTo(null);
    }

    private void initLayout() {
        // Prepares layout
        setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.FIRST_LINE_START;

        cons.gridx = 0;
        cons.gridy = 0;
        cons.insets = new Insets(5, 18, 18, 18);
        add(makeMainPanel(), cons);
    }

    private Component makeMainPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        JLabel label;
        Font title = MetroStyle.FORM_TITLE;
        Font header = MetroStyle.FORM_HEADER;
        Insets headerInsets = new Insets(5, 3, 2, 5);
        Insets textFieldInsets = new Insets(4, 5, 4, 5);
        Insets tipInsets = new Insets(3, 0, 6, 5);

        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.anchor = GridBagConstraints.FIRST_LINE_START;

        // Start adding components
        cons.gridx = 0;
        cons.gridy = 0;
        cons.gridwidth = 3;
        cons.insets = new Insets(3, 1, 10, 5);
        label = new JLabel("Edit Group");
        label.setFont(title);
        label.setForeground(MetroStyle.LIGHTER_MARINE);
        panel.add(label, cons);

        cons.gridy++;
        cons.insets = headerInsets;
        cons.gridwidth = 2;
        label = new JLabel("New group's name?*");
        label.setFont(header);
        panel.add(label, cons);

        cons.gridy++;
        cons.insets = textFieldInsets;
        panel.add(txtGroupName, cons);

        cons.gridx = 0;

        cons.gridy++;
        cons.insets = new Insets(25, 4, 4, 6);
        cons.gridwidth = 1;
        cons.weightx = 0.2;
        panel.add(btnSave, cons);

        cons.gridx++;
        cons.insets = new Insets(25, 6, 4, 4);
        cons.weightx = 0.1;
        panel.add(btnCancel, cons);

        return panel;
    }

    private void initListener() {
        btnSave.addActionListener(this);
        btnCancel.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equalsIgnoreCase("cancel")) {
            this.dispose();
        } else if (cmd.equalsIgnoreCase("save")) {

            Status status = engine.renameGroup(engine.getClient().getUID(), group.getGroupId(), txtGroupName.getText());
            if (status == Status.SUCCESS) {
                contactsView.resetFriendsList();
                this.dispose();
            }else {
                JOptionPane.showMessageDialog(this, "Cannot change group name");
            }
        }
    }
}
